home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / BROWSERS / GLUESTIK.ZOO / gsdriver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-30  |  3.3 KB  |  169 lines

  1. #include <osbind.h>
  2. #include <mintbind.h>
  3. #include <basepage.h>
  4. #include <ctype.h>
  5. #include <minimal.h>
  6. #include "cookie.h"
  7. #include "patchlev.h"
  8. #include "global.h"
  9. #include "pipe.h"
  10.  
  11. long _stksize = 65536L;
  12. int cjar_size = 0;
  13. COOKIE *new_cjar = 0;
  14. int ncookies = 0;
  15. int mint_present = 0;
  16. int stik_present = 0;
  17. int pipe_fd = 0;
  18. #if 0
  19. Daemon_Interface *DMN = 0;
  20. #endif
  21.  
  22. #ifdef __MINT__
  23. /* We define these here to keep main.o from being pulled out of libc.a */
  24. int errno;
  25. int __mint;
  26. #endif
  27.  
  28. #if 0
  29. unsigned long dehexify(register char *buf)
  30. {
  31.   register unsigned long l = 0;
  32.  
  33.   for (; *buf; buf++)
  34.     l = (l << 4) + toint(*buf);
  35.   return l;
  36. }
  37. #endif
  38.  
  39. int check_cookies(void)
  40. {
  41.   register COOKIE *cjar = *CJAR, *C;
  42.  
  43.   if (!cjar)
  44.     return 0;
  45.  
  46.   for (C = cjar; C->tag != 0; C++) {
  47.     if (C->tag == COOKIE_MiNT)
  48.       mint_present = 1;
  49.     if (C->tag == COOKIE_STiK)
  50.       stik_present = 1;
  51.   }
  52.  
  53.   cjar_size = C->value;
  54.   ncookies = C - cjar;
  55.   return 0;
  56. }
  57.  
  58. int install_same_jar(void)
  59. {
  60.   register COOKIE *C = *CJAR + ncookies;
  61.  
  62.   C->tag = COOKIE_STiK;
  63.   C->value = (long)&driver;
  64.   C[1].tag = 0;
  65.   C[1].value = cjar_size;
  66.   return 0;
  67. }
  68.  
  69. int install_new_jar(void)
  70. {
  71.   register COOKIE *oldC = *CJAR, *newC = new_cjar;
  72.  
  73.   *CJAR = newC;
  74.   while (oldC->tag != 0)
  75.     *newC++ = *oldC++;
  76.   newC->tag = COOKIE_STiK;
  77.   newC->value = (long)&driver;
  78.   newC[1].tag = 0;
  79.   newC[1].value = cjar_size + 8;
  80.   return 0;
  81. }
  82.  
  83. int install_cookie(void)
  84. {
  85.   if (ncookies < cjar_size - 1)
  86.     return Supexec(install_same_jar);
  87.   else {
  88.     if (!(new_cjar = (COOKIE *)Mxalloc((cjar_size + 8) * sizeof(COOKIE), 3))) {
  89.       Cconws("Unable to install STiK cookie\r\n");
  90.       return -1;
  91.     }
  92.     return Supexec(install_new_jar);
  93.   }
  94. }
  95.  
  96. /* cleanup() -- free everything we might have alloced, and destroy all
  97.    semaphores we might have created. */
  98. static void cleanup(void)
  99. {
  100. #if 0    /* The daemon will destroy these */
  101.   /* We have to be holding the semaphore to destroy it */
  102.   Psemaphore(2, DMN_SEMAPHORE, -1L);
  103.   Psemaphore(1, DMN_SEMAPHORE, 0L);
  104. #ifdef DEBUG
  105.   Psemaphore(2, DEBUG_SEMAPHORE, -1L);
  106.   Psemaphore(1, DEBUG_SEMAPHORE, 0L);
  107. #endif /* DEBUG */
  108. #endif /* 0 */
  109. }
  110.  
  111. int main()
  112. {
  113.   Cconws("\r\nGlueSTiK\277 STiK emulator for MiNTnet\r\n"
  114.      "Application interface driver\r\n"
  115.      "Version " GS_VERSION "\r\n"
  116.      "\275 1996 Scott Bigham\r\n");
  117. #ifdef DEBUG
  118.   Cconws("    Debug logging enabled\r\n");
  119. #endif
  120. #ifdef DEBUG
  121.   debug("s", "Starting GSDriver v" GS_VERSION);
  122. #endif
  123.  
  124. #if 0
  125.   if (argc != 2) {
  126.     Cconws("Daemon address absent --- not installing\r\n");
  127.     return -1;
  128.   }
  129.   DMN = (Daemon_Interface *)dehexify(argv[1]);
  130. #ifdef DEBUG
  131.   debug("sssX", "Ifc ptr = ", argv[1], ", magic = ", (DMN ? DMN->magic : 0L));
  132. #endif
  133.   if (!DMN || (DMN->magic != GSDAEMON_MAGIC)) {
  134.     Cconws("Daemon address invalid --- not installing\r\n");
  135.     return -1;
  136.   }
  137. #endif /* 0 */
  138.  
  139.   Supexec(check_cookies);
  140.   if (!mint_present) {
  141.     Cconws("MiNT not present --- not installing\r\n");
  142.     return -1;
  143.   }
  144.   if (stik_present) {
  145.     Cconws("STiK already present --- not installing\r\n");
  146.     return 0;
  147.   }
  148.  
  149.   if (!init_stubs()) {
  150.     cleanup();
  151.     return -1;
  152.   }
  153. #ifdef DEBUG
  154.   debug("s", "Network stub functions initialized");
  155. #endif
  156.  
  157.   if (install_cookie() < 0) {
  158.     cleanup();
  159.     return -1;
  160.   }
  161. #ifdef DEBUG
  162.   debug("s", "STiK cookie installed");
  163. #endif
  164.  
  165.   Cconws("\r\n");
  166.   Ptermres(1024L + _base->p_tlen + _base->p_dlen + _base->p_blen, 0);
  167.   return -1;
  168. }
  169.